<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <div style="width:1050px;float:left">
              <table style="font-size:12px" border="0" class="table1style">
                  <tr>
                      <td align="right">标本日期</td>
                      <td>
                          <el-date-picker v-model="sampledate" value-format="yyyy-MM-dd" :clearable="false" laceholder="请选择标本日期" style="width:100%" @change="SampledateChange"></el-date-picker>
                      </td>
                      <td align="right">报告单元</td>
                      <td>
                          <rm-ddl-rptunit prgno="LIS324" v-model="rptunitid" :clearable="false" filterable @change="RptUnitChange" defaultflag="1" :appendtobody="true" width="200px"></rm-ddl-rptunit>
                      </td>
                      <td align="right">病人来源</td>
                      <!-- <td>
                          <rm-ddl-common datatype="seccode" kindno="PT" dataname="病人来源" v-model="pattypecode" width="130px"></rm-ddl-common>
                      </td> -->
                      <td width="225" style="display: flex;">
                          <rm-ddl-common datatype="seccode" kindno="PT" dataname="病人来源" v-model="pattypecode" width="160" style="margin-right:10px;" @change="pattypeChange"></rm-ddl-common>
                          <el-button @click.native="multiFun('pat')" style="background-color:#e6e6e6;color:black">多选</el-button>
                      </td>
                      <td>
                          <el-checkbox v-model="onlyshow" true-label="1" false-label="0">仅显示无病历号或姓名的报告</el-checkbox>
                      </td>
                  </tr>
                  <tr>
                      <td align="right">标 本 号</td>
                      <td>
                          <el-input v-model="sampleno" placeholder=""></el-input>
                      </td>
                      <td colspan="2">
                          <p style="color:blue">标本号可以输入如：1,2,5-8等格式</p>
                      </td>
                      <td align="right">申请科室</td>
                      <!-- <td>
                          <rm-ddl-common datatype="dept" dataname="科室" v-model="deptno" width="130px"></rm-ddl-common>
                      </td> -->
                      <td width="225" style="display: flex;">
                          <rm-ddl-common datatype="dept" dataname="科室" v-model="deptno" width="160" style="margin-right:10px;" @change="reqdeptChange"></rm-ddl-common>
                          <el-button @click.native="multiFun('dept')" style="background-color:#e6e6e6;color:black">多选</el-button>
                      </td>
                      <td width="220" align="left">
                          <el-radio-group v-model="print_count">
                              <el-radio label="All">全部</el-radio>
                              <el-radio label="0">未打印</el-radio>
                              <el-radio label="1">已打印</el-radio>
                          </el-radio-group>
                      </td>
                  </tr>
                  <tr>
                      <td colspan="7">
                          <el-checkbox v-model="onlydelete">仅删除指定的报告项目</el-checkbox>
                          <el-input v-model="rptitemname" style="width:calc(100% - 150px)" :disabled="!onlydelete" @dblclick.native="dialogMultipleVisible = true"></el-input>
                      </td>
                      <td></td>
                  </tr>
              </table>
          </div>
          <div style="width:calc(100% - 1050px);float:left;margin-top: 90px">
              <table style="float:right">
                  <tr>
                      <td>
                          <el-button type="primary" @click="btnQuery">查询</el-button>
                      </td>
                      <td>
                          <el-button @click="DelReport(3)" v-show="UpdatePower == '1'">仅删除结果</el-button>
                      </td>
                      <td>
                          <el-button @click="DelReport(2)" v-show="DelPower == '1'">仅删除病人信息</el-button>
                      </td>
                      <td>
                          <el-button @click="DelReport(1)" v-show="DelPower == '1'">删除报告</el-button>
                      </td>
                  </tr>
              </table>

          </div>
      </div>
      <div class="pagecenter">
          <div style="height:100%;width:100%;background-color:#ddccbb;">
              <vxe-table show-overflow ref="xTable1" height="auto" :loading="loading" :cell-class-name="cellClassName" :sort-config="{trigger: 'cell'}" :data.sync="tableData" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe @current-change="currentChangeEvent" header-align="center">
                  <vxe-table-column type="selection" width="45" align="center"></vxe-table-column>
                  <vxe-table-column field="emer_flag" title="加急" width="45" sortable align="center" :formatter="['formatSelect', emerOption]"></vxe-table-column>
                  <vxe-table-column field="sampleno" title="标本编号" sortable width="70" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_name" title="姓名" width="60" sortable align="center"></vxe-table-column>
                  <vxe-table-column field="pat_sexname" title="性别" width="50" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_ageyear" title="年龄" width="50" align="center"></vxe-table-column>
                  <vxe-table-column field="req_deptnoname" title="申请科室" width="100" align="center"></vxe-table-column>
                  <vxe-table-column field="req_wardnoname" title="病人病区" width="100" align="center"></vxe-table-column>
                  <vxe-table-column field="req_bedno" title="床号" width="60" align="center"></vxe-table-column>
                  <vxe-table-column field="DelTips" title="删除提示" width="150" align="center"></vxe-table-column>
                  <vxe-table-column field="sampledate" title="标本日期" width="90" :formatter="['toDateString', 'yyyy-MM-dd']" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_no" title="病人号" width="100" header-align="center"></vxe-table-column>
                  <vxe-table-column field="specimen_codename" title="标本类型" width="70" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_typecodename" title="病人类别" width="80" align="center"></vxe-table-column>
                  <vxe-table-column field="item_num" title="项目数" width="60" align="center"></vxe-table-column>
                  <vxe-table-column field="print_count" title="打印次数" width="80" align="center"></vxe-table-column>
                  <vxe-table-column field="reportid" title="报告ID" width="110"></vxe-table-column>
                  <vxe-table-column field="rptunitid" title="报告单元ID" width="90"></vxe-table-column>
                  <vxe-table-column field="warn_flag" title="警告" width="45" align="center" :formatter="['formatSelect', WarnOption]"></vxe-table-column>
                  <vxe-table-column field="req_docno" title="申请医生" width="100" align="center"></vxe-table-column>
              </vxe-table>
          </div>
      </div>
      <!--多选弹框-->
      <!--病人来源多选-->
      <el-dialog title="病人来源多选" :visible.sync="multiPatsourceVisible" v-if="multiPatsourceVisible" width="920px" v-el-drag-dialog :close-on-click-modal="false" :destroy-on-close="true">
          <!-- <rm-transfer v-model="multiPatsource" :leftTableData="leftOptionPat" :rightTableData="rightOptionPat" @change="closedialog" name="pat"></rm-transfer> -->
          <rm-SqlMultiple v-model="multiPatsource" :leftOption="leftOptionPat" :rightOption="rightOptionPat" @change="closedialog" name="pat" ref="PatMulti"></rm-SqlMultiple>
      </el-dialog>
      <!--申请科室多选-->
      <el-dialog title="申请科室多选" :visible.sync="reqDeptVisible" v-if="reqDeptVisible" width="920px" v-el-drag-dialog :close-on-click-modal="false" :destroy-on-close="true">
          <rm-SqlMultiple v-model="reqDept" :leftOption="leftOptionDept" :rightOption="rightOptionDept" @change="closedialog" name="dept" ref="DeptMulti"></rm-SqlMultiple>
      </el-dialog>

      <!--多选弹窗--->
      <el-dialog width="600px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialogMultipleVisible" :destroy-on-close="true" title="请选择报告项目" v-if="dialogMultipleVisible">
          <rm-chkboxSelect2 prgno="LIS324" @cancel="dialogMultipleVisible=false" :isAllRpt="1" :rptunitid="rptunitid" :rptunitdisabled="true" @selitem="SelectRptItem" @selitems="SelectRptItems" ref="rptdialog"></rm-chkboxSelect2>
      </el-dialog>
  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils";
XEUtils.mixin({
  // 自定义全局的格式化处理函数
  formatSelect(cellValue, list) {
      let item = list.find((item) => item.value == cellValue);
      return item ? item.label : "";
  },
});
export default {
  name: "LIS324",
  data() {
      return {
          sampledate: "",
          rptunitid: "",
          pattypecode: "",
          sampleno: "",
          deptno: "",
          onlydelete: false,
          onlyshow: "0",
          rptitemname: "", //删除的报告项目选择
          rptitemcode: "", //删除的报告项目选择对应代码
          print_count: "All", //打印方式
          //表格配置
          loading: false,
          tableData: [],
          emerOption: [
              { label: "", value: "0" },
              { label: "急", value: "1" },
          ],
          WarnOption: [
              { label: "", value: "0" },
              { label: "警", value: "1" },
          ],
          selection: [],
          delindex: 0, //当前删除到的行
          delsuccess: 0, //删除成功
          delfail: 0,

          multiPatsource: "",
          multiPatsourceVisible: false,
          leftOptionPat: [],
          rightOptionPat: [],
          reqDept: "",
          reqDeptVisible: false,
          leftOptionDept: [],
          rightOptionDept: [],
          DelPower: "0",
          UpdatePower: "0",

          dialogMultipleVisible: false,
          SelectRptList: "",
      };
  },
  created() {
      this.InitDate(); //初始化主列表
  },
  mounted() {
      this.init();
  },
  updated() {},
  methods: {
      async init() {
          const res = await this.$tool.request({
              url: "LIS/LIS3/LIS324",
              method: "post",
              data: {
                  action: "init",
                  rptunitid: this.rptunitid
              },
          });
          this.leftOptionDept = res.deptOption;
          this.leftOptionPat = res.pattypeOption;
          this.DelPower = res.powRet;
          this.UpdatePower = res.canupdate;
      },
      InitDate() {
          this.sampledate = XEUtils.toDateString(new Date(), "yyyy-MM-dd");
      },
      QueryData() {
          this.loading = true;
          console.log("dept", this.deptno);
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS324",
                  method: "post",
                  data: {
                      action: "query",
                      rptunitid: this.rptunitid,
                      sampleno: this.sampleno,
                      deptno: this.deptno,
                      typecode: this.pattypecode,
                      sampledate: this.sampledate,
                      onlyshow: this.onlyshow,
                      print: this.print_count,
                  },
              })
              .then((response) => {
                  for (let i = 0; i < response.report.length; i++) {
                      response.report[i].DelTips = "";
                  }
                  this.tableData = response.report;
                  //this.loadFirstRow(this.$refs.xTable1, this.currentChangeEventFun);
                  this.loading = false;
              })
              .catch((e) => {
                  this.loading = false;
                  this.$message.warning(e);
              });
      },
      DelReport(mode) {
          this.selection = this.$refs.xTable1.getSelectRecords();
          this.delindex = 0;
          this.delsuccess = 0;
          this.delfail = 0;
          if (this.selection.length == 0) {
              this.$alert("未选中要删除的行!", "提示");
              return;
          }
          this.$confirm("确定要删除选中项目吗?", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              focusCancelButton: true,
              type: "warning",
          })
              .then(() => {
                  this.$rm_Login({
                      type: "请输入删除者工号和密码",
                      btn: {
                          confirmBtnText: "确定",
                          cancelBtnText: "取消",
                      },
                  })
                      .then((res) => {
                          if (res.flag == "1") {
                              var rptitemcode = "";
                              if (this.onlydelete) rptitemcode = this.rptitemcode;
                              this.DeleteReport(this.selection[0].reportid, mode, rptitemcode);
                          } else {
                              this.$alert("登录失败:");
                          }
                      })
                      .catch((e) => {
                          this.$alert("删除登录出现错误:" + e);
                      });
              })
              .catch(() => {});
      },
      DeleteReport(reportid, mode, rptitemcode) {
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS324",
                  method: "post",
                  data: {
                      action: "delete",
                      reportid: reportid,
                      mode: mode,
                      rptitemcode: rptitemcode,
                  },
              })
              .then((response) => {
                  if (response.flag == "1") {
                      this.selection[this.delindex].DelTips = "删除成功";
                      this.delsuccess++;
                      console.log(this.selection[this.delindex]);
                      if (++this.delindex < this.selection.length) {
                          this.DeleteReport(this.selection[this.delindex].reportid, mode, rptitemcode);
                      } else {
                          this.alert("选中报告删除完毕,删除成功" + this.delsuccess + "条,删除失败" + this.delfail + "条", "提示");
                      }
                  } else {
                      this.selection[this.delindex].DelTips = "删除失败:" + response.msg;
                      this.delfail++;
                      if (++this.delindex < this.selection.length) {
                          this.DeleteReport(this.selection[this.delindex].reportid, mode, rptitemcode);
                      } else {
                          this.alert("选中报告删除完毕,删除成功" + this.delsuccess + "条,删除失败" + this.delfail + "条", "提示");
                      }
                  }
              })
              .catch((e) => {});
      },
      currentChangeEvent({ row }) {},
      cellClassName({ row, column }) {
          if (column.property == "redo_flag") {
              if (row.redo_flag == "1") {
                  return "col-yellow";
              } else {
                  return "";
              }
          } else if (column.property == "emer_flag") {
              if (row.emer_flag == "1") {
                  return "col-green";
              } else {
                  return "";
              }
          } else if (column.property == "warn_flag") {
              if (row.warn_flag == "1") {
                  console.log("row.warn_flag ", row.warn_flag);
                  return "col-red";
              } else {
                  return "";
              }
          }
      },
      btnQuery() {
          this.QueryData();
      },
      //多选按钮
      async multiFun(type) {
          if (type == "pat") {
              await this.init();
              this.rightOptionPat = [];

              if (this.multiPatsource != "") {
                  let k = this.multiPatsource.split(",");
                  k.forEach((item) => {
                      let ndata = this.leftOptionPat.find((m) => m.data == item);
                      this.rightOptionPat.push(ndata);
                  });
              }
              this.multiPatsourceVisible = true;
          } else {
              await this.init();
              this.rightOptionDept = [];
              if (this.reqDept != "") {
                  let k = this.reqDept.split(",");
                  k.forEach((item) => {
                      let ndata = this.leftOptionDept.find((m) => m.data == item);
                      this.rightOptionDept.push(ndata);
                  });
              }
              this.reqDeptVisible = true;
          }
      },
      MsgCarouselChange(val) {
          this.NowMsgNo = val - 0 + 1;
      },
      SampledateChange(val) {},
      RptUnitChange(val) {
          this.rptunitid = val;
          this.init();
      },
      pattypeChange(val) {
          this.multiPatsource = val;
      },
      reqdeptChange(val) {
          this.reqDept = val;
      },
      closedialog(value, name) {
          if (name == "pat") {
              if (value != "close") {
                  this.multiPatsource = value;
                  this.pattypecode = value;
              }
              this.multiPatsourceVisible = false;
          } else {
              if (value != "close") {
                  this.reqDept = value;
                  this.deptno = value;
              }
              this.reqDeptVisible = false;
          }
      },
      SelectRptItem(value, name, oprate) {
          this.rptitemname = name;
          this.rptitemcode = value;
          this.dialogMultipleVisible = false;
      },
      SelectRptItems(rows) {
          var name = "";
          var code = "";
          for (let i = 0; i < rows.length; i++) {
              name += rows[i].rpt_itemname + ",";
              code += rows[i].rpt_itemcode + ",";
          }
          name = name.substring(0, name.length - 1);
          code = code.substring(0, code.length - 1);
          this.rptitemname = name;
          this.rptitemcode = code;
          this.dialogMultipleVisible = false;
      },
  },
};
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 130px;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 135px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
/deep/ .vxe-table .vxe-body--column.col-red {
  background-color: #990033;
}
/deep/ .vxe-table .vxe-body--column.col-yellow {
  background-color: #999900;
}
/deep/ .vxe-table .vxe-body--column.col-green {
  background-color: #00ffcc;
}

/deep/.vxe-cell .el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
/deep/.vxe-cell .el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
/deep/.vxe-cell .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
</style>